﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Com.Aote.ObjectTools;
using Com.Aote.Behaviors;
using System.Net;
using System.Linq;
using System.Json;

namespace Com.Aote.Pages
{
	public partial class 换表管理 : UserControl
	{
        String jsonjs="";
        String jsontb = "";
        String url = "";
		public 换表管理()
		{
			// Required to initialize variables
			InitializeComponent();
		}

        // 老物联表接口
        //#region saveButton_Click 保存按钮按下时
        //private void save_Click(object sender, RoutedEventArgs e)
        //{            
        //    ui_userfiles.IsBusy = true;
        //    jsontb = "[{customer_code:\"" + ur_userid.Text + "\"}]";
        //    jsonjs = "[{userid:" + ur_userid.Text + ",reading:" + lastinputgasnum_cb.Text + ",lastreading:" + lastinputgasnum.Text + "}]";
        //    url = "/handcharge/record/batch/" + ui_handdate.SelectedDate + "/" + ui_sgnetwork.Text + "/" + ui_sgoperator.Text + "/" + chaobiaoriqi.SelectedDate + "/" + meter.SelectedValue.ToString() + "/false" + "?uuid=" + System.Guid.NewGuid().ToString();
        //    BatchExcuteAction save = (from p in loader.Res where p.Name.Equals("CreateHandplan") select p).First() as BatchExcuteAction;
        //    save.Completed += save_Completed;
        //    save.Invoke();
            
        //}

        //void save_Completed(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        //{
        //    BatchExcuteAction c = sender as BatchExcuteAction;
        //    c.Completed -= save_Completed;

        //    //计算抄表费用
        //    WebClientInfo wci1 = Application.Current.Resources["server"] as WebClientInfo;
        //    string uri1 = wci1.BaseAddress + url;
        //    WebClient client1 = new WebClient();
        //    client1.UploadStringCompleted += client1_UploadStringCompleted;
        //    client1.UploadStringAsync(new Uri(uri1), jsonjs);
           
        //}

        //void client1_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        //{
        //    WebClient client1 = sender as  WebClient;
        //    client1.UploadStringCompleted -= client1_UploadStringCompleted;

        //    BatchExcuteAction save1 = (from p in loader.Res where p.Name.Equals("SaveAction") select p).First() as BatchExcuteAction;
        //    save1.Completed += save1_Completed;
        //    save1.Invoke();
        //}

        //void save1_Completed(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        //{
        //    BatchExcuteAction c = sender as BatchExcuteAction;
        //    c.Completed -= save_Completed;
        //    //同步 将产生的json串送后台服务进行处理
        //    WebClientInfo wci = Application.Current.Resources["server"] as WebClientInfo;
        //    string uri = wci.BaseAddress + "/iesgas/user/comand";
        //    WebClient client = new WebClient();
        //    client.UploadStringCompleted += client_UploadStringCompleted;
        //    client.UploadStringAsync(new Uri(uri), jsontb);
        //}

        //void client_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        //{
        //    ui_userfiles.IsBusy = false;
        //    if (e.Error == null)
        //    {
        //        //弹出错误信息
        //        if (e.Result != "")
        //        {
        //            // MessageBox.Show(e.Result);
        //        }

        //    }
        //}
        //#endregion

        private void TextBox_LostFocus_1(object sender, RoutedEventArgs e)
        {
            //{m:Exp Str=tabletel.IsOld\=True}
            if (null != f_meternumber.Text && !"".Equals(f_meternumber.Text))
            {
                GeneralObject save2 = (from p in loader.Res where p.Name.Equals("tabletel") select p).First() as GeneralObject;
                save2.isBusy = true;
                save2.Path = "one/select new Map(meter_phone as meter_phone,terminal_name as terminal_name) from t_meterinfo  where f_meternumber='" + f_meternumber.Text + "'";
                save2.Load();
            }
        }

        #region saveButton_Click 保存按钮按下时
        private void saveButton_Click(object sender, RoutedEventArgs e)
        {
            ui_userfiles.IsBusy = true;
            if (null != ui_newaliasname.Text && "物联网表".Equals(ui_newaliasname.Text))
            {
                //同步 将产生的json串送后台服务进行处理 
                //string jsonstr="{\"userid\":\""+ur_userid.Text+"\",\"oldnum\":\""+lastinputgasnum_cb.Text+"\",\"newnum\":\""+f_newgasmeteraccomodations.Text+"\",\"gaswatchbrand\":\""+CoboxPinpai.SelectedValue.ToString()+"\"}";
                WebClientInfo wci = Application.Current.Resources["huanbiaourl"] as WebClientInfo;
                string uri = wci.BaseAddress + "";
                WebClient client12 = new WebClient();
                client12.UploadStringCompleted += client12_UploadStringCompleted;
                client12.UploadStringAsync(new Uri(uri), "{data:{f_filiale:\"" + ur_filiale.Text + "\",userid:\"" + ur_userid.Text + "\",oldnum:" + lastinputgasnum_cb.Text + ",newnum:" + f_newgasmeteraccomodations.Text + ",gaswatchbrand:\"" + ui_newaliasname.Text + "\",oldgaswatchbrand:\"" + ui_aliasname.Text + "\"}}");
            }
            else
            {
                // 清除界面数据
                BatchExcuteAction save11 = (from p in loader.Res where p.Name.Equals("SaveAction") select p).First() as BatchExcuteAction;
                save11.Completed += save11_Completed;
                save11.Invoke();
            }

        }

        void client12_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            WebClient client12 = sender as WebClient;
            client12.UploadStringCompleted -= client12_UploadStringCompleted;
            // 没有出错
            if (e.Error == null)
            {
                try
                {
                    String status = e.Result as String;
                    JsonObject retjson = null;
                    if (status.IndexOf("{") >= 0 || status.IndexOf("}") >= 0)
                    {
                        //返回 json {数据}，status 0成功，1失败，info记录错误信息 userid,status，info
                        retjson = JsonValue.Parse(status) as JsonObject;
                        status = retjson["status"];
                    }
                    if ("0".Equals(status))
                    {
                        
                    }
                    else
                    {
                        MessageBox.Show("用户ID：" + retjson["userid"] + " 换表成功，但用户余额不够下账，请进行一次机表交费更新用户账务， \n返回状态：" + retjson["status"] + " \n错误信息：" + retjson["message"] + " \n账户结余：" + retjson["zhye"].ToString() + "元");                        
                    }
                    // 清除界面数据
                    BatchExcuteAction save11 = (from p in loader.Res where p.Name.Equals("SaveAction") select p).First() as BatchExcuteAction;
                    save11.Completed += save11_Completed;
                    save11.Invoke();

                }
                catch (Exception ex)
                {
                    ui_userfiles.IsBusy = false;
                }
            }
            else
            {
                // 提示出错
                MessageBox.Show("连接服务器失败，请重试！如果继续失败，请联系系统管理员。");
                ui_userfiles.IsBusy = false;
            }

        }

        void save11_Completed(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            BatchExcuteAction save11 = sender as BatchExcuteAction;
            save11.Completed -= save11_Completed;
            ui_userfiles.IsBusy = false;
        }

        #endregion


	}
}